package com.hyperfix.models.washer.pojos;

import lombok.Data;

import com.hyperfix.utils.common.NumberFormatter;

/**
 * @author JiangChunYu
 * 2024/3/19
 */
@Data
public class ParamsData {
    private int stationId;
    private int pipelineId;
    private double temperature;
    private double pressure;
    private double oilPressure;
    private double casingPressure;
    private int diffPressure;
    private int flow;
    private int totalFlow;
    private byte status;

    /**
     * 解析数据
     *
     * @param data 编码后的数据
     */
    public ParamsData(byte[] data) {
        this.stationId = data[0] >>> 1;
        this.pipelineId = ((data[0] & 0x00000001) << 8) | data[1];

        this.temperature = (data[3] & 0x0000007F) * 0.01 + data[2];
        if ((data[3] & 0x00000080) != 0) this.temperature = -this.temperature;

        this.pressure = (data[5] & 0x0000007F) * 0.01 + data[4];
        if ((data[5] & 0x00000080) != 0) this.pressure = -this.pressure;

        this.oilPressure = (data[7] & 0x0000007F) * 0.01 + data[6];
        if ((data[7] & 0x00000080) != 0) this.oilPressure = -this.oilPressure;

        this.casingPressure = (data[9] & 0x0000007F) * 0.01 + data[8];
        if ((data[9] & 0x00000080) != 0) this.casingPressure = -this.casingPressure;

        this.diffPressure = ((data[10] << 7) & 0x00007F80) | ((data[11] >>> 1) & 0x0000007F);
        if ((data[11] & 1) == 1) this.diffPressure = -this.diffPressure;

        this.flow = ((data[12] << 8) & 0x0000FF00) | (data[13] & 0x000000FF);

        this.totalFlow = ((data[14] << 16) & 0x00FF0000) | ((data[15] << 8) & 0x0000FF00) | (data[16] & 0x000000FF);

        this.temperature = NumberFormatter.formatDouble(this.temperature, 2);
        this.pressure = NumberFormatter.formatDouble(this.pressure, 2);
        this.oilPressure = NumberFormatter.formatDouble(this.oilPressure, 2);
        this.casingPressure = NumberFormatter.formatDouble(this.casingPressure, 2);
    }
}
